package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import java.util.ArrayList;
import java.util.List;
import com.example.myapplication3.MyAdapter.Holder;
import com.example.myapplication3.MyAdapter.OnContactItemIconClick;
public class MainActivity extends AppCompatActivity {
    // private static final int request_code_get_content = 0xf1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        String name = Singleton.getInstance().getName();
    }

    private void initView() {
        //获取ListView
        ListView contactList = findViewById(R.id.list_contact);
        //构造数据
        List<ContactModel> data = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            ContactModel model = new ContactModel();
            model.setContactName("张三" + i);
            model.setIconResId(R.mipmap.ic_launcher);
            data.add(model);
        }
        //创建适配器Adpate
        MyAdapter adapter = new MyAdapter(this, data);
        //将适配器adapter赋值给ListView
        contactList.setAdapter(adapter);
        adapter.setListener(new MyAdapter.OnContactItemIconClick() {
        @Override
        public void onContactIconClick(int i) {
            Toast.makeText(MainActivity.this,
                    "点击了 " + data.get(i).getContactName() + "的头像",
                    Toast.LENGTH_SHORT).show();
                }
            });


                //ListView的条目点击事件
                contactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    //position 列表角标，对应的数据List的角标
                    //view即适配器adapter中getView（）返回的列表条目对象
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view,
                                            int i, long l) {
                        //ListView列表条目点击事件的目的是：
                        //获取用户点击的条目所对应的数据List中的数据
                        String name = data.get(i).getContactName();
                        Toast.makeText(MainActivity.this, "联系人姓名" + name,
                                Toast.LENGTH_SHORT).show();
                    }
                });
        //ListView列表条目长按事件
        contactList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                String name = data.get(i).getContactName();

                Toast.makeText(MainActivity.this,
                        "联系人姓名：" + name,
                        Toast.LENGTH_SHORT).show();
                //需要返回true
                return true;
            }
        });
    }
}


    /*private void initView(){
        Button btnToInner = findViewById(R.id.btn_to_child);
        Button btnToOuuer = findViewById(R.id.btn_to_outter);
        Button btnGetcontent = findViewById(R.id.btn_get_content);

        btnGetcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,ChildActivity.class);

                //Activity跳转回传
                //参数1：intent
                //参数2：requestCode 跳转回传的入口标识，用于不同的跳转方向
           startActivityForResult(intent,request_code_get_content);
            }
        });

        btnToOuuer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转外部Activity时，需要通过外部app暴露出来的Activity的Action作为标识
                Intent intent = new Intent("com.tencent.wachat.share.ACTION_PYQ");
                //标注目标外部APP的包名
                intent.setPackage("com.example.myapplication2");
                //防止外部App未启动导致无法跳转到外部App的Activity页面
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                //发起跳转
                startActivity(intent);
            }
        });

        btnToInner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转内部Activity
                //参数1：当前Activity对象
                //参数2：目标Activity的class对象
                //Intent设置需要跳转的起点和目标，并且可携带数据到目标
                Intent intent = new Intent(MainActivity.this, ChildActivity.class);

                //Intent传值
                //传递数据方法1,直接调用Intent.putExtra()
                intent.putExtra("main_data","hello Child");


                //传递数据方法2,通过getExtra()获取到Bundle对象之后，通过Bundle.putXXX()传递数据
                //intent.getExtras().putString("main_data","hello Child");
                //跳转Activity
                startActivity(intent);


            }
        });


    }*/
